import os
import json

if __name__ == "__main__":
    PATH = "../../User/Das"
else:
    PATH = "./User/Das"

print(PATH)


class InterfaceChara:
    """
        指定用户的角色的添加与删除
    """

    def __init__(self, username):
        self.user = username
        self.userDas = PATH + f"/{username}"
        print(os.listdir(PATH))
        if username not in os.listdir(PATH):
            self.create_user_folder()

    def add_new_chara(self, code):
        listChara = self.get_chara_list()
        listChara.append(code)
        listChara = list(set(listChara))
        self.save_list(listChara)

    def save_list(self, list):
        with open(self.userDas + "/chara_list.json", 'w') as f:
            return json.dump(list, f)

    def is_has_chara(self, code):
        list_chara = self.get_chara_list()
        return code in list_chara

    def get_chara_list(self) -> list:
        with open(self.userDas + "/chara_list.json", 'r') as f:
            return json.load(f)

    def create_user_folder(self):
        """
        给新用户创建一个存档点
        @return:
        """

        here = os.getcwd()
        os.chdir(PATH)
        os.mkdir(self.user)
        with open(f"./{self.user}/chara_list.json", 'w') as f:
            json.dump([], f)

        os.chdir(here)


if __name__ == "__main__":
    test = InterfaceChara("TestUser")
    test.add_new_chara(3)
    test.add_new_chara(3)
    test.add_new_chara(4)
    test.add_new_chara(2)
    print(test.is_has_chara(3))
